package com.tanhua.manager.service;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.tanhua.domain.db.UserInfo;
import com.tanhua.domain.mongo.Comment;
import com.tanhua.domain.mongo.Publish;
import com.tanhua.domain.mongo.Video;
import com.tanhua.domain.vo.CommentVO;
import com.tanhua.domain.vo.PageResult;
import com.tanhua.domain.vo.PublishVO;
import com.tanhua.domain.vo.VideoVO;
import com.tanhua.dubbo.api.CommentApi;
import com.tanhua.dubbo.api.PublishApi;
import com.tanhua.dubbo.api.UserInfoApi;
import com.tanhua.dubbo.api.VideoApi;
import org.apache.dubbo.config.annotation.Reference;
import org.joda.time.DateTime;
import org.springframework.beans.BeanUtils;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * @author liuyp
 * @date 2021/02/21
 */
@Service
public class UserService {

    @Reference
    private UserInfoApi userInfoApi;
    @Reference
    private PublishApi publishApi;
    @Reference
    private VideoApi videoApi;
    @Reference
    private CommentApi commentApi;

    /**
     * 分页查询用户列表，查询的是tanhua库里的tb_user_info表
     * @param page
     * @param pagesize
     * @return
     */
    public ResponseEntity findUserList(int page, int pagesize) {
        //1. 远程调用api，分页查询用户列表
        IPage<UserInfo> iPage = userInfoApi.findUserList(page, pagesize);

        //2. 转换成PageResult对象
        PageResult<UserInfo> pageResult = new PageResult<>();
        pageResult.setCounts((int) iPage.getTotal());
        pageResult.setPage(page);
        pageResult.setPagesize(pagesize);
        pageResult.setPages((int) iPage.getPages());
        pageResult.setItems(iPage.getRecords());

        return ResponseEntity.ok(pageResult);
    }

    public ResponseEntity findUserInfoById(Long userId) {
        //1. 远程调用api，查询用户详情
        UserInfo userInfo = userInfoApi.findById(userId);
        return ResponseEntity.ok(userInfo);
    }

    /**
     * 分页查询某一用户的动态列表
     * @param page 页码
     * @param pagesize 查询几条
     * @param uid 用户id
     * @param state 动态的审核状态
     */
    public ResponseEntity findMovementsList(int page, int pagesize, Long uid, Integer state) {
        //1. 远程调用API，分页查询动态列表
        PageResult<Publish> pageResult = publishApi.findPublishListByState(page, pagesize, uid, state);

        //2. 转换成VO
        List<Publish> publishList = pageResult.getItems();
        List<PublishVO> voList = new ArrayList<>();
        for (Publish publish : publishList) {
            PublishVO vo = new PublishVO();

            //封装动态发布者的信息
            UserInfo userInfo = userInfoApi.findById(publish.getUserId());
            BeanUtils.copyProperties(userInfo, vo);
            vo.setTags(userInfo.getTags().split(","));

            //封装动态信息
            BeanUtils.copyProperties(publish, vo);
            vo.setId(publish.getId().toHexString());
            vo.setImageContent(publish.getMedias().toArray(new String[]{}));
            vo.setDistance("50米");
            vo.setCreateDate(new DateTime(publish.getCreated()).toString("yyyy-MM-dd HH:mm:ss"));

            voList.add(vo);
        }

        //3. 构造返回值
        PageResult<PublishVO> voPageResult = new PageResult<>();
        BeanUtils.copyProperties(pageResult, voPageResult);
        voPageResult.setItems(voList);
        return ResponseEntity.ok(voPageResult);
    }

    public ResponseEntity findVideoList(int page, int pagesize, Long uid) {
        //1. 远程调用API，分页查询视频列表
        PageResult<Video> pageResult = videoApi.findVideoListByUid(page, pagesize, uid);

        //2. 转换成VO
        List<Video> videoList = pageResult.getItems();
        List<VideoVO> voList = new ArrayList<>();
        for (Video video : videoList) {
            VideoVO vo = new VideoVO();

            //把视频信息封装到vo
            BeanUtils.copyProperties(video, vo);
            vo.setId(video.getId().toHexString());
            vo.setCover(video.getPicUrl());
            vo.setSignature(video.getText());


            //把视频发布者信息封装到VO
            UserInfo userInfo = userInfoApi.findById(video.getUserId());
            vo.setAvatar(userInfo.getAvatar());
            vo.setNickname(userInfo.getNickname());

            voList.add(vo);
        }

        //3. 构造返回值
        PageResult<VideoVO> voPageResult = new PageResult<>();
        BeanUtils.copyProperties(pageResult, voPageResult);
        voPageResult.setItems(voList);
        return ResponseEntity.ok(voPageResult);
    }

    public ResponseEntity findMovementById(String publishId) {
        //1. 查询publishId对应的动态
        Publish publish = publishApi.findById(publishId);

        //2. 转换成VO
        PublishVO vo = new PublishVO();
        //封装动态发布者的信息
        UserInfo userInfo = userInfoApi.findById(publish.getUserId());
        BeanUtils.copyProperties(userInfo, vo);
        vo.setTags(userInfo.getTags().split(","));

        //封装动态信息
        BeanUtils.copyProperties(publish, vo);
        vo.setId(publish.getId().toHexString());
        vo.setImageContent(publish.getMedias().toArray(new String[]{}));
        vo.setDistance("50米");
        vo.setCreateDate(new DateTime(publish.getCreated()).toString("yyyy-MM-dd HH:mm:ss"));

        return ResponseEntity.ok(vo);
    }

    public ResponseEntity findMovementCommentsList(int page, int pagesize, String messageID) {
        //1. 远程调用api，查询评论列表
        PageResult<Comment> pageResult = commentApi.findCommentList(page, pagesize, messageID);

        //2. 转换成VO
        List<Comment> commentList = pageResult.getItems();
        List<CommentVO> voList = new ArrayList<>();
        for (Comment comment : commentList) {
            CommentVO vo = new CommentVO();

            BeanUtils.copyProperties(comment, vo);
            vo.setId(comment.getId().toHexString());
            vo.setCreateDate(new DateTime(comment.getCreated()).toString("yyyy-MM-dd HH:mm:ss"));

            UserInfo userInfo = userInfoApi.findById(comment.getUserId());
            vo.setAvatar(userInfo.getAvatar());
            vo.setNickname(userInfo.getNickname());

            voList.add(vo);
        }

        //3. 构造返回值
        PageResult<CommentVO> voPageResult = new PageResult<>();
        BeanUtils.copyProperties(pageResult, voPageResult);
        voPageResult.setItems(voList);
        return ResponseEntity.ok(voPageResult);
    }
}
